import {Injectable} from '@nestjs/common';
import {InjectRepository} from "@nestjs/typeorm";
import {Repository} from "typeorm";
import {OrderItemEntity} from "./order-item.entity";
import {ResultVo} from "../../vo/common/result.vo";

@Injectable()
export class OrderItemService {
    constructor(
        @InjectRepository(OrderItemEntity)
        private readonly orderItemRepository: Repository<OrderItemEntity>,
    ) {}

    async queryAll() {
        let data = await this.orderItemRepository.find()
        return ResultVo.successWithData(data)
    }

    async queryOne(id: number | string) {
        let data = await this.orderItemRepository.findOne(id)
        return ResultVo.successWithData(data)
    }

    async add(data: OrderItemEntity) {
        let date = new Date()
        data.createdTime = date
        data.updatedTime = date
        let res = await this.orderItemRepository.save<OrderItemEntity>(data)
        return ResultVo.successWithData(res)
    }

    async edit(data: OrderItemEntity) {
        data.updatedTime = new Date()
        let res = await this.orderItemRepository.update({id:data.id},data)
        return ResultVo.successWithData(res)
    }

    async delete(id: number| string) {
        let res = await this.orderItemRepository.delete(id)
        return ResultVo.successWithData(res)
    }
}
